package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ywk
 * @date: 2023-11-19
 */
public class P530 {
    public static void main(String[] args) {

    }

    public static int getMinimumDifference(TreeNode root) {
        List<TreeNode> result = new ArrayList<>();
        min(root, result);
        int min = Integer.MAX_VALUE;
        if(result.size()==2){
            return result.get(1).val - result.get(0).val;
        }
        for (int i = 1; i < result.size()-1; i++) {
            int diffPre = result.get(i).val - result.get(i - 1).val;
            int diffNext = result.get(i+1).val - result.get(i).val;
            int minTemp = Math.min(diffPre, diffNext);
            min = Math.min(min, minTemp);
        }
        return min;
    }
    public static void min(TreeNode root,List<TreeNode> result) {
        if (root == null) {
            return;
        }
        min(root.left,result);
        result.add(root);
        min(root.right, result);
    }
}
